 global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_A9_alm_sectors.log, replace name(fig)

capture noi {
 
    
    * v2
    * List sectors with highest and lowest shares of automation patents in machinery
    * ---------------------------------
    * Correlation scatters
    * ---------------------------------

    global cvers "_ipc4"

    use ${final_dir}/ind6090_patents_regression_data${cvers}.dta, clear

    * Merge in capital purchase
    mmerge ind6090 using ${final_dir}/capital_purchases_ind6090.dta, unmatched(master)

    * Figure template
    qui include ${code_dir}/config/figuretools.do
    local ind_symb "mcol(%30) mlw(thin)"
    local ind_lab "mlab(ind6090_outl) mlabsize(vsmall) mlabc(gs8) msym(i)"
    local ind_lab2 "mcol(%30) mlw(thin)" 
    local lfit lcolor(gs4) lwidth(medthick)
    local legend "legend(lab(1 "Industries") lab(3 "") lab(2 "Linear fit") rows(1))"
    tostring ind6090, replace

    cap program drop flag_outliers_abs
    program flag_outliers_abs
        cap drop ind6090_outl
        gen ind6090_outl = " "
        gsort -`1' ind6090
        replace ind6090_outl = ind6090 if _n <= 6
        sort `1' ind6090
        replace ind6090_outl = ind6090 if _n <= 6
        gsort -`2' ind6090
        replace ind6090_outl = ind6090 if _n <= 6
        sort `2' ind6090
        replace ind6090_outl = ind6090 if _n <= 6
    end

    cap program drop flag_outliers
    program flag_outliers
        cap drop resid ind6090_outl
        predict resid, residuals
        sort resid
        gen ind6090_outl1 = " "
        replace ind6090_outl = ind6090 if _n <= 5
        gsort -resid
        replace ind6090_outl = ind6090 if _n <= 5
        sort ind6090
    end


    foreach wvers in weighted { 
        preserve
        
        local weight "[aw = avwt]"

        * A: log Auto95 vs log Pauto90

        gen lnpauto90_use = ln(pauto90_use)
        gen lnpauto90_use_shifted = lnpauto90_use + 0.0005
        gen lnauto95_use = ln(auto95_use)

        reg lnauto95_use lnpauto90_use `weight', vce(cluster uas_ind6090) 
        local coef1 : di %3.1f _b[lnpauto90_use]
        local se1 : di %3.1f _se[lnpauto90_use]
        corr lnauto95_use lnpauto90_use `weight' 
        local corr1 : di %3.2f el(r(C),1,2)

        flag_outliers
        
        tw (scatter lnauto95_use lnpauto90_use `weight', ///
        `ind_symb' ///
        ytitle("Log auto95") xtitle("Log pauto90") ylab(#4)) ///
        (scatter lnauto95_use lnpauto90_use_shifted, `ind_lab') ///
        || lfit lnauto95_use lnpauto90_use `weight', ///
        `lfit' ///
        text(0 9 "Corr.  `corr1'", color(gs5) just(left)) /// 
        `legend' ///
        title("(A)  Correlation of auto95 and pauto90", position(6)) legend(off) ///
        name(lnpauto90, replace)

        * B: Auto95/cap vs Pauto90/cap

        gen auto95_use_cap = auto95_use / capitalp97
        gen pauto90_use_cap = pauto90_use / capitalp97

        flag_outliers 

        reg auto95_use_cap pauto90_use_cap `weight', vce(cluster uas_ind6090) 
        local coef2 : di %3.1f _b[pauto90_use_cap]
        local se2 : di %3.1f _se[pauto90_use_cap]
        corr auto95_use_cap pauto90_use_cap `weight'
        local corr2 : di %3.2f el(r(C),1,2)

        tw (scatter auto95_use_cap pauto90_use_cap `weight', ///
        `ind_symb' ///
        ytitle("Auto95/K") xtitle("Pauto90/K") ylab(#4)) ///
        (scatter auto95_use_cap pauto90_use_cap, `ind_lab') ///
        || lfit auto95_use_cap pauto90_use_cap `weight', ///
        `lfit' ///
        text(0.007 .69 "Corr.  `corr2'", color(gs5) just(left)) /// 
        `legend' ///
        title("(B)  Correlation of auto95/K and pauto90/K", position(6)) legend(off) ///
        name(pauto90_cap, replace)

        * Combine 
        grc1leg2 lnpauto90 pauto90_cap, xsize(12) ysize(7) mtsize(small)
        graph export ${fig_dir}/appendix/Figure_A9_alm_sector_corr_`wvers'.pdf, replace
        graph export ${fig_dir}/appendix/Figure_A9_alm_sector_corr_`wvers'.eps, replace
        restore
    }

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig
